package engine.logic
{
	import engine.control.MenuList;
	import engine.data.ModelLocator;
	import engine.data.ObjectData;
	import engine.data.View;
	import engine.data.ViewObject;
	import engine.event.SoundEvent;
	import engine.flag.GameDefine;
	import engine.net.datatype.WString;
	import engine.net.messages.CustomToServerFlag;
	import engine.net.mgr.SystemSender;
	import engine.newUi.ActorPropertyLogic;
	import engine.newUi.ComposeItemLogic;
	import engine.newUi.DepositoryLogic;
	import engine.newUi.FriendLogic;
	import engine.newUi.IntensifyLogic;
	import engine.newUi.MyPropertyLogic;
	import engine.newUi.NoticeUI;
	import engine.newUi.PackLogic;
	import engine.newUi.PetLoic;
	import engine.newUi.PetUI;
	import engine.newUi.SellLogic;
	import engine.newUi.SkillStree;
	import engine.newUi.SpecialKungFuLogic;
	import engine.newUi.SpecialKungfuInfoLogic;
	import engine.newUi.SpecialKungfuInfoUI;
	import engine.newUi.WhisperLogic;
	import engine.newUi.WorldMapLogic;
	import engine.scene.SceneUtil;
	import engine.ui.AlertUI;
	import engine.ui.chat.ChatUI;
	
	import flash.display.MovieClip;
	
	import newx.com.config.StaticIni;


	public class MenuListManager
	{
		/**
		 *	点击物品 
		 * @param logic		所在logic
		 * @param dataObj	obj
		 * 
		 */

		public static function clickItem( logic : Class, dataObj : ViewObject ):void
		{
			if( logic == DepositoryLogic )
			{//仓库
				clickDepotItem( dataObj );
			}else if( logic == PackLogic )
			{//背包
				clickPackItem( dataObj );
			}else if( logic == MyPropertyLogic )
			{//人物
				clickEquipItem( dataObj );
			}
			else if(logic == PetLoic)
			{//宠物装备
				clickPetEquipItem(dataObj);
			}
		}
		
		/**
		 *	点击玩家 
		 * @param player
		 * 
		 */		
		public static function clickPlayer( name:String):void
		{
			var content : Array = [];
			
			//比武
//			content.push( "比武" );	
//			//查看
			content.push( StaticIni.getSwfLangStr("UIString_1000368"));//"查看" );	
			
			if(FriendLogic.getInstance().CanWhisperAndFriend==true)
			{
				//加好友
				content.push(StaticIni.getSwfLangStr("UIString_1000082"));//"添加好友");
				//私聊
			 	content.push(StaticIni.getSwfLangStr("Common_Whisper"));//"私聊");
			}
			
			
			showMenuList( content, onClickPlayerBack, [ content, name ] );
		}
		
		private static function onClickPlayerBack( clickIndex:uint, dataObj:Object ):void
		{
			var clickContent 	:	String 		=	dataObj[ 0 ][ clickIndex ];
			var name			:	String		=	dataObj[ 1 ];
			
			if( "比武" ==  clickContent )
			{
				SystemSender.getInstance().CustomMsg(
					CustomToServerFlag.CLIENT_CUSTOMMSG_PK, new WString( name ) );			
			}else if( StaticIni.getSwfLangStr("UIString_1000368")	==	clickContent)//"查看"
			 {
				ActorPropertyLogic.getInstance().lookOver(name);
			 }
			else if(StaticIni.getSwfLangStr("UIString_1000082") == clickContent)//"添加好友" 
			{
				FriendLogic.getInstance().sendAddFriend(name);
			}
			else if(StaticIni.getSwfLangStr("Common_Whisper") == clickContent)//"私聊"
			{
				WhisperLogic.getInstance().whisperSart(name);
			}
		}
		/**
		 * 点击派名竞技场的人名 
		 * @param name
		 * 
		 */		
		public static function clickPkListName(name:String):void
		{
			var content : Array = [];
			
			//"查看"
			content.push(  StaticIni.getSwfLangStr("UIString_1000368") );	//"查看"
			if(FriendLogic.getInstance().CanWhisperAndFriend == true)
			{
				//"私聊" 
				content.push( StaticIni.getSwfLangStr("Common_Whisper"));	//"私聊" 
				//好友
				content.push( StaticIni.getSwfLangStr("Common_Friend") );	//"好友"
			}
			
			
			
			
			showMenuList( content, onClickPKlistBack, [ content, name ] );
		}
		/**
		 * 点击江湖玩家名字*********************************************
		 * @param name
		 * 
		 */	
		public static function clickNotepadName(name:String):void
		{
			var content : Array = [];
			
			//比武
			content.push( "打招呼" );	
			//查看
			content.push( StaticIni.getSwfLangStr("UIString_1000082") );	//"添加好友"
			if(FriendLogic.getInstance().CanWhisperAndFriend==true)
			{
				//好友
				content.push( StaticIni.getSwfLangStr("Common_Whisper") );//"私聊"
			}
				
			
			
			
			showMenuList( content, onClickNotepadlistBack, [ content, name ] );
		}
		/**
		 * 点击江湖玩家名字 发送消息*********************************************
		 * @param name
		 * 
		 */	
		public static function clickNotepadCall(name:String):void
		{
			var content : Array = [];
			
			//愤怒
			content.push( StaticIni.getSwfLangStr("PlayerFace_001") );	//"愤怒"
			//微笑
			content.push( StaticIni.getSwfLangStr("PlayerFace_002") );	//"微笑"
			//鄙视
			content.push( StaticIni.getSwfLangStr("PlayerFace_003") );	//"鄙视"
			//高兴
			content.push( StaticIni.getSwfLangStr("PlayerFace_004") );	//"高兴"
			
			showMenuList( content,clickNotepadCalllistBack, [ content, name ] );
		}
		private static function clickNotepadCalllistBack(clickIndex:uint, dataObj:Object):void 
		{
			var clickContent 	:	String 		=	dataObj[ 0 ][ clickIndex ];
			var name			:	String		=	dataObj[ 1 ];
			if(StaticIni.getSwfLangStr("PlayerFace_001") == clickContent)
			{//"愤怒" 
				SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLEINT_CUSTOMMSG_KNIGHT_CALL ,new WString( name ),1);
				
			}
			else if(StaticIni.getSwfLangStr("PlayerFace_001")  == clickContent)
			{//"微笑"
				SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLEINT_CUSTOMMSG_KNIGHT_CALL ,new WString( name ),2);
			}
			else if(StaticIni.getSwfLangStr("PlayerFace_001")  == clickContent)
			{//"鄙视"
				SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLEINT_CUSTOMMSG_KNIGHT_CALL ,new WString( name ),3);
			}
			else if (StaticIni.getSwfLangStr("PlayerFace_001") == clickContent)
			{//"高兴"
				SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLEINT_CUSTOMMSG_KNIGHT_CALL ,new WString( name ),4);
			}
		}
		/**
		 * 点击江湖玩家名字*********************************************
		 * @param name
		 * 
		 */	
		private static function onClickNotepadlistBack(clickIndex:uint, dataObj:Object):void
		{
			var clickContent 	:	String 		=	dataObj[ 0 ][ clickIndex ];
			var name			:	String		=	dataObj[ 1 ];
			
			if("打招呼" == clickContent)
			{
				
				SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLEINT_CUSTOMMSG_KNIGHT_CALL ,new WString( name ),5);
				
			}
			else if(StaticIni.getSwfLangStr("UIString_1000082") == clickContent)
			{//"添加好友"
				FriendLogic.getInstance().sendAddFriend(name);
			}
			else if(StaticIni.getSwfLangStr("Common_Whisper")  == clickContent)
			{//"私聊"
			//	FriendLogic.getInstance().WhispChat();
				
			}
		}
		/**
		 *`点击仓库道具 
		 * @param item
		 * 
		 */
		private static function clickDepotItem( item : ViewObject ):void
		{
			var content : Array = [];
			
			//取出
			if( PackLogic.getInstance().ui.created )
			{//"取出"
				content.push( StaticIni.getSwfLangStr("UIStr_1000526") );	
			}
			
			//展示
			content.push( StaticIni.getSwfLangStr("UIStr_1000591") );	
			
			showMenuList( content, onClickItemBack, [ content, item ] );
		}
		
		/**
		 *	点击装备的道具 
		 * 
		 */
		private static function clickEquipItem( item : ViewObject ):void
		{
			var content : Array = [];
			
			//卸下
			content.push( StaticIni.getSwfLangStr("UIStr_1000623"));	
			//展示
			content.push( StaticIni.getSwfLangStr("UIStr_1000591") );	
			
			showMenuList( content, onClickItemBack, [ content, item ] );
		}
		/**
		 *点击宠物装备栏装备 
		 * @param item
		 * 
		 */		
		private static function clickPetEquipItem(item : ViewObject):void
		{
			var content : Array = [];
			
			//卸下
			content.push( StaticIni.getSwfLangStr("UIStr_1000623"));	
			//展示
			content.push( StaticIni.getSwfLangStr("UIStr_1000591") );	
			
			showMenuList( content, onClickItemBack, [ content, item ] );
		}
		/**
		 *	点击背包道具 
		 * @param item
		 * 
		 */
		private static function clickPackItem( item : ViewObject ):void
		{
			var content : Array = [];
			
			//存入
			if( DepositoryLogic.getInstance().ui.created )
			{
				content.push(  StaticIni.getSwfLangStr("UIStr_1000525") );//"存入"
			}
			
			//出售
			if( SellLogic.getInstance().ui.created )
			{
				if(item.GetProperties("CanSell")==1)
				{
					content.push(  StaticIni.getSwfLangStr("UIStr_1000132"));//"出售" 
				}
				
			}
//			trace("MenuListManager.clickPackItem(item)",item.GetPropertiesEx("ItemType"));
			switch(item.GetPropertiesEx("ItemType"))
			{
				//装备
				case GameDefine.ItemType_PET_EUIP:
				{
					content.push(  StaticIni.getSwfLangStr("UIStr_1000131") );	//"装备"
					content.push( StaticIni.getSwfLangStr("UIStr_1000195"));//"升级"
					break;
				}
				case GameDefine.ItemType_EUIP:
				{//"装备"
					content.push(  StaticIni.getSwfLangStr("UIStr_1000131") );	
					break;
				}
					//合成卷轴
				case GameDefine.ItemType_COMPOSE:
				{
					content.push( StaticIni.getSwfLangStr("DailyEventButton_1")  );//"打开"
					content.push(  StaticIni.getSwfLangStr("NewItemChangeUIStr_3"));//"合成" 
					break;
				}
					//药品
				case GameDefine.ItemType_health:
				{
					content.push( StaticIni.getSwfLangStr("UIString_1000645") );	
					break;
				}
					//材料
				case GameDefine.ItemType_Material:
				{
					break;
				}
					//绝学残章
				case GameDefine.ItemType_SpecialKungfu:
				{
					content.push(  StaticIni.getSwfLangStr("UIString_1000368") );
					break;
				}
					//令牌
				case GameDefine.ItemType_TOKEN:
				{
					break;
				}
					//任务物品 102
				case GameDefine.ItemType_RASKRES:
				{
					
					break ; 
				}
				default:
				{//"使用"
					content.push(  StaticIni.getSwfLangStr("UIStr_1001332") );
					break;
				}
			}
			//展示
			content.push(  StaticIni.getSwfLangStr("UIStr_1000591") );	
			//丢弃
			if(item.GetPropertiesEx("ItemType")!=GameDefine.ItemType_GOLDBOX)
				content.push(  StaticIni.getSwfLangStr("UIStr_1000355") );//"丢弃"
			
			showMenuList( content, onClickItemBack, [ content, item ] );
		}
		
		private static function onClickItemBack( clickIndex:uint, dataObj:Object ):void
		{
			var clickContent 	:	String 		=	dataObj[ 0 ][ clickIndex ];
			var itemObj			:	ViewObject	=	dataObj[ 1 ]; 
			
			if(  StaticIni.getSwfLangStr("UIStr_1000525") == clickContent )
			{//存入
				SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_MOVEITEM,
					View.VIEWPORT_TOOL, int( itemObj.ObjKey ),
					View.VIEWPORT_STORE, 0 );	
			}else if(  StaticIni.getSwfLangStr("UIStr_1000131") == clickContent )
			{//"装备"
				if(itemObj.GetProperties("ItemType") ==GameDefine.ItemType_EUIP)
				{
					PackLogic.getInstance().equip( itemObj );
					if( !MyPropertyLogic.getInstance().ui.created )
					{
						MyPropertyLogic.getInstance().ui.showOrHide();
					}
					ModelLocator.getInstance().dispatchEvent(new SoundEvent(SoundEvent.EQUIP_UP));
				}
				else if(itemObj.GetProperties("ItemType") ==GameDefine.ItemType_PET_EUIP)
				{
					var viewID:int=-1;
					if(PetUI.getInstance().created)
					{
						 viewID =PetLoic.getInstance().currPetView;
						if(viewID!=-1)
						{
							PackLogic.getInstance().equip( itemObj ,viewID);
						}
						else
						{
							NoticeUI.getInstance().showInfo(StaticIni.getSwfLangStr("UIString_1000646"));
						}
					}
					else
					{
						var congfigid:int =ModelLocator.getInstance().scene.Role.GetProperties("FightPet");
						 viewID =PetLoic.getInstance().byCoinfigidFindViewID(congfigid);
						 PetUI.getInstance().showOrHide();
						if(viewID!=-1)
						{
						
							var currFightPet:ViewObject =PetLoic.getInstance().byCoinIDCkeckPet(congfigid);
							if(currFightPet ==null)
							{
								NoticeUI.getInstance().showInfo(StaticIni.getSwfLangStr("UIString_1000646"));
								return;
							}
							PetLoic.getInstance().bySelcetIndexUpdate(currFightPet);
							PackLogic.getInstance().equip( itemObj ,viewID);
						}
						else
						{
							var petVo:View		=	ModelLocator.getInstance().scene.GetView(View.VIRWPORT_PET);
							NoticeUI.getInstance().showInfo(StaticIni.getSwfLangStr("UIString_1000646"));
						}
					}
					
				}
				
			}
			else if( StaticIni.getSwfLangStr("DailyEventButton_1") == clickContent)
			{//"打开"
				ComposeItemLogic.getInstance().SetItem(itemObj,1,View.VIEWPORT_TOOL,0);
				
			}
			else if( StaticIni.getSwfLangStr("UIStr_1000195") == clickContent)
			{//"升级"
				ComposeItemLogic.getInstance().SetItem(itemObj,1,View.VIEWPORT_TOOL,0);
			}
			else if( StaticIni.getSwfLangStr("UIString_1000368")  ==clickContent )
			{
				//查看
				if(itemObj.GetPropertiesEx("ItemType") == GameDefine.ItemType_SpecialKungfu)
				{
					var id:int = int(StaticIni.getIniVar(itemObj.GetProperties("ConfigID"),"PlayerCategory"));
					var stree:SkillStree = SpecialKungFuLogic.getInstance().getKungfuByID(id);
					SpecialKungfuInfoLogic.getInstance().currSkill = stree;
				}
			}
			else if( StaticIni.getSwfLangStr("UIString_1000645")	==	clickContent)
			{
				//表示服用的是丹药
				if(itemObj.GetProperties("SubItemType") == 10402 )
				{
					IntensifyLogic.getInstance().useIntensify(itemObj.GetProperties("ConfigID"),1);
					return;
				}
				if(!roleConfigID)
				{
					SystemSender.getInstance().
						CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_PILL_PLAYER_USE,int(itemObj.GetProperties("ConfigID")))
				}
				else
				{
					SystemSender.getInstance().
						CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_PILL_PARTNER_USE,int(itemObj.GetProperties("ConfigID")),roleConfigID)
				}
			}
			else if( StaticIni.getSwfLangStr("F_Gem_13203") == clickContent)
			{//"合成"
				ComposeItemLogic.getInstance().onSendCompose(itemObj,1,View.VIEWPORT_TOOL,int(itemObj.ObjKey),1);
			}
			else if(  StaticIni.getSwfLangStr("UIStr_1000132") == clickContent )
			{//"出售"
				PackLogic.getInstance().sellEquip( itemObj );
			}
			else if(  StaticIni.getSwfLangStr("UIStr_1000554") == clickContent )
			{//"使用"
				if(querp(itemObj.GetProperties("ConfigID")))
				{
					SystemSender.getInstance().
						CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_PILL_PLAYER_USE,itemObj.GetProperties("ConfigID"))
				}
				SystemSender.getInstance().CustomMsg( CustomToServerFlag.CLIENT_CUSTOMMSG_USEITEM,View.VIEWPORT_TOOL, int( itemObj.ObjKey ) );
				
			}else if(  StaticIni.getSwfLangStr("UIStr_1000591") == clickContent )
			{//"展示"
				ChatUI.getInstance().showItem( itemObj, View.VIEWPORT_TOOL );
			}else if(  StaticIni.getSwfLangStr("UIStr_1000623") == clickContent )
			{//"卸下"
				MyPropertyLogic.getInstance().demountEquip( itemObj );
				ModelLocator.getInstance().dispatchEvent(new SoundEvent(SoundEvent.EQUIP_DOWN));
	
//				var	pos : int =	GameDefine.EQPOS_HASH_MAP[ itemObj.Properties[ "EquipType" ] ];
//				if( 0 != pos )
//				{
//					SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_MOVEITEM,
//						MyPropertyLogic.getInstance().getEquipView(), pos,	
//						View.VIEWPORT_TOOL, 0 );	
//				}
			}else if(  StaticIni.getSwfLangStr("UIStr_1000526") == clickContent )
			{//"取出"
				SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_MOVEITEM,
					View.VIEWPORT_STORE, int( itemObj.ObjKey ),
					View.VIEWPORT_TOOL, 0);
			}else if(  StaticIni.getSwfLangStr("UIStr_1000355") == clickContent )
			{//"丢弃"
				if(itemObj.CheckProperties("Amount")  && itemObj.GetPropertiesEx("Amount") > 1)
				{
					var amount:String = itemObj.GetProperties("Amount");
					AlertUI.showAlert(StaticIni.getSwfLangStr("UIStr_1000355"),StaticIni.getSwfLangStrVar("UIStr_1000440",
						[" "+ SceneUtil.getItemName( itemObj.GetPropertiesEx("ConfigID") ) + " " ]),AlertUI.YES|AlertUI.NO,
					confirmDiscard,[itemObj,int( itemObj.ObjKey )],true,300,150,amount);
				}else
				{
					AlertUI.showAlert(StaticIni.getSwfLangStr("UIStr_1000355"),StaticIni.getSwfLangStrVar("UIStr_1000439",
						[" "+ SceneUtil.getItemName( itemObj.GetPropertiesEx("ConfigID") ) + " " ]),AlertUI.YES|AlertUI.NO,
					confirmDiscard,[itemObj,int( itemObj.ObjKey )]);
				}
			}
			
			function confirmDiscard(clickName:uint,inputTxt:String,clickData:Array):void
			{
				if(clickName==AlertUI.YES)
				{
					var tObj:ObjectData = clickData[0];
					if(tObj.CheckProperties("Amount") && tObj.GetPropertiesEx("Amount") > 1 )
					{
						if(uint(inputTxt)!=0)
						{
							var amount:int = tObj.GetProperties("Amount");
							var saleAmount:int	=	uint(inputTxt);
							var temAmount:int = (amount >= saleAmount)?0:saleAmount;
							SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_CHUCKITEM,
								View.VIEWPORT_TOOL,clickData[1],saleAmount);
						}
					}else{
						SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_CHUCKITEM,
							View.VIEWPORT_TOOL,clickData[1],0);
					}
				}
			}
			
			function querp(val:int):Boolean
			{
				var allArr:Array	=	[34010100,34010200,34010300,34010400,34010500,34010600,
										 34020100,34020200,34020300,34020400,34020500,34020600,
										 34030100,34030200,34030300,34030400,34030500,34030600];
				for(var i:int=0; i < allArr.length;i++)
				{
					if(val == allArr[i])
					return true;
				}
				return false;
			}
		}
		/**
		 * 排名竞技场名字点击 
		 * @param clickIndex
		 * @param dataObj
		 * 
		 */		
		private static function onClickPKlistBack(clickIndex:uint, dataObj:Object):void
		{
			var clickContent 	:	String 		=	dataObj[ 0 ][ clickIndex ];
			var itemObj			:	String		=	dataObj[ 1 ];
			if( StaticIni.getSwfLangStr("UIString_1000368")	==	clickContent)
			{//"查看"
				ActorPropertyLogic.getInstance().lookOver(itemObj);
			}
			else if( StaticIni.getSwfLangStr("Common_Friend")	==	clickContent)
			{//"好友"
				FriendLogic.getInstance().sendAddFriend(itemObj);
//				SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_REQUEST,GameDefine.REQUESTTYPE_FRIEND,
//					new WString(itemObj));
			}
			else if( StaticIni.getSwfLangStr("Common_Whisper")	==	clickContent)
			{//"私聊"
				WhisperLogic.getInstance().whisperSart(itemObj);
//				ChatUI.getInstance().setWisper(itemObj);
			}
		}
		
		public static function clickWorldMapScene(sceneID:Array):void
		{
			var content : Array = [];
			content.push(StaticIni.getSwfLangStr("SceneNameStr"+sceneID[0]));
			content.push(StaticIni.getSwfLangStr("SceneNameStr"+sceneID[1]));
			//			content.push("高级地图");
			//			content.push("低级地图");
			showMenuList(content,onClickChooseWorldMapSceneItem,[content,sceneID]);
		}
		
		public static function onClickChooseWorldMapSceneItem(clickIndex:uint, dataObj:Object):void
		{
			var sceneID			:	Array		=	dataObj[1];
			var sceneMc : MovieClip = WorldMapLogic.getInstance().getSceneMc(sceneID[clickIndex]);
			WorldMapLogic.getInstance().startMove(sceneMc);
		}
		
		public static  function get roleConfigID():int
		{
			if(MyPropertyLogic.getInstance().ui.created)
			{
				return (MyPropertyLogic.getInstance().currentSelectedM["dataobj"] as ObjectData).GetPropertiesEx("ConfigID");
			}
			else
			{
				return 0
			}
		}
		
		/**
		 * 显示menuList
		 * @param contentList	显示的内容
		 * @param callBack		回调
		 * @param clickData		参数
		 * 
		 */		
		private static function showMenuList( contentList : Array, callBack : Function, clickData : Object ):void
		{
			MenuList.ShowMenu( contentList, callBack, clickData );
		}
	}
}